package com.lovo.pz.role;

import com.lovo.pz.util.GameUtil;
import com.lovo.pz.window.GamePanel;

import java.awt.*;
import java.util.List;
/**
 * 豌豆射手子弹角色
 * @author 白德柯
 *
 */
public class PeaShooterBulletRole extends BaseRole {
    public PeaShooterBulletRole(Component component){
        super(component);
    }
    @Override
    public void initRole() {
        setRoleWidth(GameUtil.peaShooterBulletImage.getWidth(getComponent()));
        setRoleHeight(GameUtil.peaShooterBulletImage.getHeight(getComponent()));
        setMoveLength(10);
    }

    @Override
    public void paintRole(Graphics g) {
        if(getRunStatus() == BOM){
            g.drawImage(GameUtil.peaShooterBulletBomImage,getRoleX(),getRoleY(),getComponent());
        }else{
            g.drawImage(GameUtil.peaShooterBulletImage,getRoleX(),getRoleY(),getComponent());
        }
    }

    @Override
    public void action() {
        GamePanel gamePanel = (GamePanel) getComponent();
        if(getRunStatus() == BOM){
            // 如果子弹爆炸了则等待500毫秒后消失
            if(GameUtil.delay(500,this,"BOM")){
                setRunStatus(ROLE_END);
            }
        }else{
            // 检测子弹是否碰撞到僵尸
            List<ZombieRole> zombieRoleList = gamePanel.getZombieRoleList();
            for(int i = 0;i < zombieRoleList.size();i++){
                if(GameUtil.checkImpact(this,zombieRoleList.get(i))){
                    zombieRoleList.get(i).setHp(zombieRoleList.get(i).getHp() - 1);
                    setRunStatus(BaseRole.BOM);
                }
            }
            setRoleX(getRoleX() + getMoveLength());
            if(getRoleX() > 900){
                setRunStatus(ROLE_END);
            }
        }
    }
}
